<%--
   For com.royal.art
   Copyright [2015/11/13] By RICK
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="../../common/base_body_header.jsp" %>
<%@include file="../../common/navbar.jsp" %>
<%@include file="../../common/page_content_pre.jsp" %>

<div class="page-content">

    <div class="row">
        <div class="col-sm-12">
            <div class="widget-box">
                <div class="widget-header">
                    <h4>${not empty sysUser.userId?'修改':'添加'}用户</h4>
                </div>

                <div class="widget-body">
                    <div class="widget-main no-padding">
                        <form:form class="form-horizontal" modelAttribute="sysUser"
                                   action="${GLOBAL.basePath}/sysuser/save" method="post" id="sysUserForm">
                            <form:hidden path="userId"/>
                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">所属门店：</label>

                                    <c:choose>
                                        <c:when test="${user.deptId == '1'}">
                                            <div class="col-sm-4">
                                                <select class="input-xlarge" name="deptId">
                                                    <c:forEach items="${depts}" var="item" varStatus="var">
                                                        <option value="${item.deptId}"
                                                                <c:if test="${item.deptId == sysUser.deptId}">selected</c:if>>${item.deptName}</option>
                                                    </c:forEach>
                                                </select>
                                            </div>
                                        </c:when>
                                        <c:when test="${sysUser.deptId == null}">
                                            <div class="col-sm-4">
                                                <select class="input-xlarge" name="deptId">
                                                    <c:forEach items="${depts}" var="item" varStatus="var">
                                                        <c:if test="${item.deptId == user.deptId}">
                                                            <option value="${item.deptId}">${item.deptName}</option>
                                                        </c:if>
                                                    </c:forEach>
                                                </select>
                                            </div>
                                        </c:when>
                                        <c:otherwise>
                                            <div class="col-sm-4">
                                                <select class="input-xlarge" name="deptId">
                                                    <c:forEach items="${depts}" var="item" varStatus="var">
                                                        <c:if test="${item.deptId == sysUser.deptId}">
                                                            <option value="${item.deptId}">${item.deptName}</option>
                                                        </c:if>
                                                    </c:forEach>
                                                </select>
                                            </div>
                                        </c:otherwise>
                                    </c:choose>
                                </div>
                            </fieldset>

                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">用户姓名：</label>

                                    <div class="col-sm-5">
                                        <form:input path="username" htmlEscape="false" maxlength="50"
                                                    class="input-xlarge required" placeholder="请输入用户名"/>
                                        <span class="help-inline"><font color="red">*</font> </span>
                                    </div>
                                </div>
                            </fieldset>

                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">联系方式：</label>

                                    <div class="col-sm-4">
                                        <input id="oldAccount" name="oldAccount" type="hidden"
                                               value="${sysUser.mobile}">
                                        <form:input path="mobile" htmlEscape="false" maxlength="50"
                                                    class="input-xlarge required" placeholder="请输入用户账户"/>
                                        <span class="help-inline"><font color="red">*</font> </span>
                                    </div>
                                </div>
                            </fieldset>

                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">职位：</label>

                                    <div class="col-sm-4">
                                        <select class="input-xlarge" name="userRole"
                                                onchange="show_menu(this.options[this.options.selectedIndex].value)">
                                            <c:forEach items="${allRoles}" var="item" varStatus="var">
                                                <option value="${item.roleId}"
                                                        <c:if test="${item.roleId == sysUser.userRole}">selected</c:if>>${item.roleName}</option>
                                            </c:forEach>
                                        </select>
                                    </div>
                                </div>
                            </fieldset>

                            <c:if test="${empty sysUser.userId}">
                                <fieldset>
                                    <div class="form-group">
                                        <label class="col-sm-3 control-label">密码：</label>

                                        <div class="col-sm-5">
                                            <input class="input-xlarge ${empty sysUser.userId?'required':''}" id="s_pwd"
                                                   name="password" type="password" maxlength="50" minlength="3"
                                                   placeholder="请输入密码"/>
                                            <c:if test="${empty sysUser.userId}"><span class="help-inline"><font
                                                    color="red">*</font> </span></c:if>
                                            <c:if test="${not empty sysUser.userId}"><span
                                                    class="help-inline">若不修改密码，请留空。</span></c:if>
                                        </div>
                                    </div>
                                </fieldset>
                                <fieldset>
                                    <div class="form-group">
                                        <label class="col-sm-3 control-label">确认密码：</label>

                                        <div class="col-sm-5">
                                            <input class="input-xlarge" id="s_repeat_pwd" name="repeatPwd"
                                                   type="password"
                                                   value="" maxlength="50" minlength="3" placeholder="请输入确认密码"
                                                   equalTo="#s_pwd"/>
                                            <c:if test="${empty sysUser.userId}"><span class="help-inline"><font
                                                    color="red">*</font> </span></c:if>
                                        </div>
                                    </div>
                                </fieldset>
                            </c:if>

                            <c:if test="${not empty sysUser.userId}">
                                <fieldset>
                                    <div class="form-group">
                                        <label class="col-sm-3 control-label">创建时间：</label>

                                        <div class="col-sm-6">
                                            <label class="lbl"><fmt:formatDate value="${sysUser.createDate}"
                                                                               type="both" dateStyle="full"/></label>
                                        </div>
                                    </div>
                                </fieldset>
                            </c:if>

                            <fieldset>
                                <div class="form-group">
                                    <label class="col-sm-3 control-label">用户授权：</label>
                                    <div class="col-sm-4">
                                        <div id="menuTree" class="ztree" style="margin-top:3px;float:left;"></div>
                                        <form:hidden path="menuIds"/>
                                    </div>
                                </div>
                            </fieldset>

                            <div class="form-actions center">
                                <button type="button" class="btn btn-sm btn-grey" id="go_back_btn">
                                    返回
                                    <i class="icon-reply icon-on-right bigger-110"></i>
                                </button>
                                <shiro:hasPermission name='sys:user:add'>
                                    <button type="submit" class="btn btn-sm btn-success" id="save_btn">
                                        保存
                                        <i class="icon-save icon-on-right bigger-110"></i>
                                    </button>
                                </shiro:hasPermission>
                            </div>
                        </form:form>
                    </div>
                </div>
            </div>
        </div>

    </div>

</div>

<%@include file="../../common/page_content_suf.jsp" %>
<%@include file="../../common/script.jsp" %>
<%@include file="../../common/treeview.jsp" %>

<script type="text/javascript">
    //展览下拉选项
    ART.mLoadSelectOption("${GLOBAL.basePath}/order/exhibitionList", {}, $("#s_pItemKey"), "id", "title", true);

    $(document).ready(function () {
        $("#mobile").focus();
        $("#sysUserForm").validate({
            rules: {
                mobile: {
                    remote: {
                        type: "POST",
                        cache: false,
                        url: "${GLOBAL.basePath}/sysuser/checkAccount",
                        data: {
                            oldAccount: function () {
                                return "${sysUser.mobile}";
                            }
                        }
                    }
                }
            },
            messages: {
                mobile: {required: "用户登录名不能为空", remote: jQuery.format("用户登录名已存在")},
                confirmNewPassword: {equalTo: "输入与上面相同的密码"}
            },
            submitHandler: function (form) {
                var ids = [], nodes = tree.getCheckedNodes(true);
                for (var i = 0; i < nodes.length; i++) {
                    ids.push(nodes[i].id);
                }
                $("#menuIds").val(ids);
                loading('正在提交，请稍等...');
                form.submit();
            },
            errorContainer: "#messageBox",
            errorPlacement: function (error, element) {
                $("#messageBox").text("输入有误，请先更正。");
                if (element.is(":checkbox") || element.is(":radio") || element.parent().is(".input-append")) {
                    error.appendTo(element.parent().parent());
                } else {
                    error.insertAfter(element);
                }
            }
        });

        var setting = {
            check: {enable: true, nocheckInherit: true}, view: {selectedMulti: false},
            data: {simpleData: {enable: true}}, callback: {
                beforeClick: function (id, node) {
                    tree.checkNode(node, !node.checked, true, true);
                    return false;
                }
            }
        };

        // 用户-菜单
        var zNodes = [
                <c:forEach items="${menuList}" var="menu">{
                id: "${menu.menuId}",
                pId: "${not empty menu.parent.menuId?menu.parent.menuId:0}",
                name: "${not empty menu.parent.menuId?menu.name:'权限列表'}"
            },
            </c:forEach>];
        // 初始化树结构
        var tree = $.fn.zTree.init($("#menuTree"), setting, zNodes);
        // 不选择父节点
        tree.setting.check.chkboxType = {"Y": "ps", "N": "s"};
        // 默认选择节点
        var ids = "${sysUser.menuIds}".split(",");
        for (var i = 0; i < ids.length; i++) {
            var node = tree.getNodeByParam("id", ids[i]);
            try {
                tree.checkNode(node, true, false);
            } catch (e) {
            }
        }
        // 默认展开全部节点
        tree.expandAll(true);

        $("#go_back_btn").on("click", function () {
            goBack();
        });
    });

    function goBack() {
        document.location.href = "${GLOBAL.basePath}/sysuser/list";
    }

    function show_menu(value) {
        $.get("./menuList?id=" + value, function (dataaa, status) {
            var setting = {
                check: {enable: true, nocheckInherit: true}, view: {selectedMulti: false},
                data: {simpleData: {enable: true}}, callback: {
                    beforeClick: function (id, node) {
                        tree.checkNode(node, !node.checked, true, true);
                        return false;
                    }
                }
            };
            // 用户-菜单
            var zNodes = [];

            for (var i = 0; i < dataaa.length; i++) {
                var menu = dataaa[i];
                zNodes[i] = {
                    id: menu.menuId,
                    pId: menu.parent.menuId != null ? menu.parent.menuId : 0,
                    name: menu.parent.menuId != null ? menu.name : '权限列表'
                }
            }

            // 初始化树结构
            var tree = $.fn.zTree.init($("#menuTree"), setting, zNodes);
            // 不选择父节点
            tree.setting.check.chkboxType = {"Y": "ps", "N": "s"};
            // 默认选择节点
            var ids = "${sysUser.menuIds}".split(",");
            for (var i = 0; i < ids.length; i++) {
                var node = tree.getNodeByParam("id", ids[i]);
                try {
                    tree.checkNode(node, true, false);
                } catch (e) {
                }
            }
            // 默认展开全部节点
            tree.expandAll(true);
        });
    }
</script>
